#include "http-loader.hh"
#include <ctype.h>
#include <strings.h>
#include <iostream>
string HttpLoader::PORT("80");

array<string,2> HttpLoader::HTTP_PREFIX = {"HTTP://", "HTTPS://"};

HttpLoader::HttpLoader(string url) : url_(url){
  port_ = PORT;
  parse_params_by_url();
}

string HttpLoader::START_FILE = "index.html";

void HttpLoader::parse_params_by_url(){
  string url(url_);
  for(unsigned int i = 0; i < HTTP_PREFIX.size(); i++)
    if(url.length() >= HTTP_PREFIX[i].length())
      if (!strcasecmp(url.substr(0, HTTP_PREFIX[i].length()).c_str(),
		      HTTP_PREFIX[i].c_str()))
	{
	  url = url.erase(0, HTTP_PREFIX[i].length());
	  break;
	}
  if (url.find('/') == string::npos)
    url += "/";
  if (url[url.length() - 1] == '/')
    url += START_FILE;
  int t = url.find_first_of('/');
  host_ = url.substr(0, t);
  file_ = url.substr(t, url.length() - t);

  t = url.find_last_of('/');
  output_file_ = url.substr(t + 1, url.length() - t - 1);
}
